﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Molia.Images
{
    public class ImageHelper
    {
        protected byte[] Image;
        public ImageHelper(string File)
        {
            try
            {
                var fs = new System.IO.FileStream(File, System.IO.FileMode.Open);
                var Image = new byte[5];
                fs.Read(Image, 0, Image.Length);
                fs.Close();
                if (Image[0] == 0x42 && Image[1] == 0x4d)
                {
                    Extension = ImageType.Bmp;
                }
                else if (Image[0] == 0x89 && Image[1] == 0x50 && Image[2] == 0x4e && Image[3] == 0x47)
                {
                    Extension = ImageType.Png;
                }
                else if (Image[0] == 0xff && Image[1] == 0xd8 && Image[2] == 0xff && Image[3] == 0xe0)
                {
                    Extension = ImageType.Jpeg;
                }
                else if (Image[0] == 0x49 && Image[1] == Image[0] && Image[2] == 0x2a && Image[3] == 0)
                {
                    Extension = ImageType.TIFF;
                }
                else if (Image[0] == 0x4d && Image[1] == Image[0] && Image[2] == 0 && Image[3] == 0x2a)
                {
                    Extension = ImageType.TIFF;
                }
                else if (Image[0] == 0x47 && Image[1] == 0x49 && Image[2] == 0x46 && Image[3] == 0x38 && Image[4] == 0x39)
                {
                    Extension = ImageType.Gif;
                }
                else
                {
                    throw new NotSupportedException("不支持该文件类型");
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        public ImageType Extension { get; set; }
    }
}
